//
//  YZMyStocksEditCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/30.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMyStocksEditCell.h"
#import "YZMAppStyle.h"
#import "YZMSavedStock.h"
#import <Masonry/Masonry.h>

@interface YZMyStocksEditCell ()

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *codeLabel;
@property (nonatomic, strong) UIButton *topButton;
@property (nonatomic, strong) UIButton *selectButton;


@end

@implementation YZMyStocksEditCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle: style reuseIdentifier: reuseIdentifier];
    if (self) {
        [self createObjects];
        self.leftMargin = 10.0;
        self.rightMargin = 10.0;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.multipleSelectionBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"announcement"]];
    }
    return self;
}

- (void)setRecord:(YZMSavedStock *)record{
    _record = record;
    self.nameLabel.text = record.name;
    self.codeLabel.text = record.code;
    self.selectButton.selected = NO;
}

- (void)createObjects{
    
    UIView *bgView = [UIView new];
    self.backgroundView = bgView;
    
    self.selectButton = [UIButton new];
    [self.selectButton setImage:[UIImage imageNamed:@"icon_square_unselected"] forState:UIControlStateNormal];
    [self.selectButton setImage:[UIImage imageNamed:@"icon_square_selected"] forState:UIControlStateSelected];
    [self.selectButton addTarget:self action:@selector(selectButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:self.selectButton];
    [self.selectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bgView);
        make.left.equalTo(bgView).offset(15);
        make.height.width.mas_equalTo(24);
    }];
    
    self.nameLabel = [YZMAppStyle labelWithMediumWhite];
    [self.contentView addSubview: self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).with.offset(16);
        make.bottom.equalTo(self.contentView.mas_centerY).with.offset(4);
        make.top.greaterThanOrEqualTo(self.contentView).with.offset(2);
    }];
    
    self.codeLabel  =[YZMAppStyle labelWithSmallLightGray];
    [self.contentView addSubview: self.codeLabel];
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_centerY).with.offset(4);
        make.leading.equalTo(self.nameLabel);
        make.width.equalTo(self.nameLabel);
        make.bottom.lessThanOrEqualTo(self.contentView).with.offset(-2);
    }];
    
    self.topButton = [UIButton buttonWithType: UIButtonTypeCustom];
    [self.contentView addSubview: self.topButton];
    [self.topButton setImage: [UIImage imageNamed: @"icon_red_top"] forState: UIControlStateNormal];
    [self.topButton addTarget: self action: @selector(topBtnPressed:) forControlEvents: UIControlEventTouchUpInside];
    [self.topButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.trailing.equalTo(self.mas_centerX).with.offset(40);
        make.width.height.mas_equalTo(26);
    }];
}

- (void)topBtnPressed:(UIButton *)sender{
    
    if (self.topAction) {
        
        self.topAction(_record);
    }
}

- (void)selectButtonPressed:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    if (self.selectAction) {
        
        self.selectAction(sender.selected,_record);
    }
}

@end
